<?php

namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {
        $this->display();
    }

    /**
     * 登录
     */
    public function login()
    {
        $pass = M("pass");
        $post = I("post.");



        $where = array("ps_phone" => $post["phone"], "ps_pass" => encodepass($post["pass"]));
        $info = $pass->where($where)->find();
        if ($info["ps_id"] == null) {
            $this->error("用户名或密码错误");
        }
        $user = $this->getuserinfo($post["phone"]);
        $ssion = array("phone" => $post["phone"], "type" => $user["type"], "sdk" => null);
        if ($user["type"] == "11" || $user["type"] == "12" || $user["type"] == "13") {
            session("stock_code", $ssion);
        } else {
            if($post["is_remember"] == 1){
                cookie("user_affline",$ssion);
            }
            session("userAffline", $ssion);
        }
        switch ($user["type"]) {
            case '11':header("Location:http://ocss.yubao21.com/Home/Index/ssidtoken/phone/" . $ssion["phone"] . "/type/" . $ssion["type"]);
                break;
            case '12':header("Location:http://ocss.yubao21.com/Home/Index/ssidtoken/phone/" . $ssion["phone"] . "/type/" . $ssion["type"]);
                break;
            case '13':header("Location:http://ocss.yubao21.com/Home/Index/ssidtoken/phone/" . $ssion["phone"] . "/type/" . $ssion["type"]);
                break;
            case '21':$this->redirect("Bord/index");
                break;
            case '23':$this->redirect("Bord/index");
                break;
            case '2301':$this->redirect("Group/index");
                break;
            case '2302':$this->redirect("Group/index");
                break;
            default: $this->redirect("Index/index");
        }
    }

    /**
     * 退出
     */
    public function out()
    {
        session("userAffline", null);
        $this->redirect("Index/index");
    }

    /**
     * 忘记密码
     */
    public function forget()
    {
        $this->display();
    }


    /**
     * 图片验证码
     */
    public function verify(){
        $Verify = new \Think\Verify( array(
            'fontSize'    =>    30,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
        ));
        $Verify->entry();
    }



    public function createsmsc()
    {
        $post = I("post.phone");

      /*  if(sentsms("2133", "亲爱的用户，您的手机验证码是：2133，此验证码5分钟内有效，请尽快完成验证。'", "18683250949")){
            echo "ok";die;
        };
        echo "no";
        die;*/
        /*$verify = new \Think\Verify();
        if(!$verify->check(I("post.verify"))){
            $this->ajaxReturn(19); //用户不存在
        }*/

        $now = time();
        if ($now > session("cuttime") || session("cuttime") == null) {
            $user = $this->getuserinfo($post);
            if ($user["phone"] == null) {
                $this->ajaxReturn(10); //用户不存在
            }
            $rand = rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9);
            $data = '亲爱的用户，您的手机验证码是：' . $rand . '，此验证码5分钟内有效，请尽快完成验证。';
            if (sentsms($rand, $data, $post)) {
                session("cuttime", $now + 600);
                $this->ajaxReturn(1);
            } else {
                $this->ajaxReturn(2);
            }
        }
        $this->ajaxReturn(0); //时间未到
    }

    public function forgetdil($p)
    {
        if (session("userAffline") == null) {
            $this->redirect("Index/index");
        }
        $smstovic = M("smstovic");
        $code = rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9);
        $data = array("stc_code" => $code, "stc_phone" => $p, "stc_time" => time());
        if ($smstovic->add($data)) {
            $this->redirect("Index/forgetup", array("dir" => $p, "code" => $code));
        }
        $this->error("系统错误");
    }

    public function forgetup($dir = null, $code = null)
    {
        $smstovic = M("smstovic");
        $post = $dir == null ? I("post.") : array("phone" => $dir, "vir" => $code);

        $where = array("stc_phone" => $post["phone"], "stc_code" => $post["vir"]);
        $info = $smstovic->where($where)->find();
        if ($info["stc_time"] == null) {
            $this->error("验证码错误!");
        }
        if ((time() - $info["stc_time"]) > 900) {
            $this->error("验证码过期了!");
        }
        $this->assign("token", $post["phone"]);
        $this->display();
    }

    public function forgetuppost()
    {
        $pass = M("pass");
        $account = M("account", "so_", C("STOCK"));
        $post = I("post.");
        if ($post["pass"] != $post["vir"]) {
            $this->error("两次密码不一致");
        }
        $info = $pass->where("ps_phone='" . $post["token"] . "'")->save(array("ps_pass" => encodepass($post["pass"])));
        if (!$info) {
            $this->error("更新密码失败!");
        }
        $account->where("a_phone='" . $post["token"] . "'")->save(array("a_password" => encodepass($post["pass"])));
        $this->redirect("Index/index");
    }

    /**
     * 获得初始密码
     */
    public function startpass()
    {
        $this->display();
    }

    /**
     * 初始密码信息
     */
    public function getstartpas()
    {
        $pass = M("pass");
        $account = M("account", "so_", C("STOCK"));
        $post = I("post.phone");

        $user = $this->getuserinfo($post);
        if ($user["phone"] == null) {
            $this->ajaxReturn(10); //用户不存在
            exit;
        }
        $info = $pass->where("ps_phone='" . $post . "'")->find();
        if ($info["ps_pass"] != null) {
            $this->ajaxReturn(9); //用户已有初始密码
            exit;
        }
        $rand = rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9);
        $arr = array("ps_phone" => $post, "ps_pass" => encodepass($rand));
        if (!$pass->add($arr)) {
            $this->ajaxReturn(8); //添加初始密码失败
            exit;
        }
        $account->where("a_phone='" . $post . "'")->save(array("a_password" => $arr["ps_pass"]));
        if (sentsms($rand, "您的初始密码是:" . $rand, $post)) {
            $this->ajaxReturn(1);
        } else {
            $this->ajaxReturn(0);
        }
    }

    /**
     * 获取用户信息
     * @param type $p
     * @return int
     */
    private function getuserinfo($p)
    {
        $salestaff = M("salestaff");
        $saleagent = M("saleagent");
        $account = M("account", "so_", C("STOCK"));

        $arr = array();
        $temp = $saleagent->where("sag_phone='" . $p . "'")->find();
        if ($temp != null) {
            $arr["phone"] = $temp["sag_phone"];
            $arr["type"] = $temp["sag_type"];
            return $arr;
        }
        $info = $salestaff->where("ssa_phone='" . $p . "'")->find();
        if ($info != null) {
            $arr["phone"] = $info["ssa_phone"];
            $arr["type"] = $info["ssa_type"];
            return $arr;
        }
        $acc = $account->where("a_phone='" . $p . "'")->find();
        $arr["phone"] = $acc["a_phone"];
        $arr["type"] = 100;
        return $arr;
    }
}
